package cn.zmit.kuxi.activity.binner;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.activity.page.DotView;

import com.xdroid.functions.request.JsonData;

public abstract class SliderBannerController {

    private SliderBanner mSliderBanner;
    private InnerAdapter mBannerAdapter = new InnerAdapter();
    @SuppressWarnings("unused")
	private DotView mDotView;
    
    public SliderBannerController(SliderBanner sliderBanner) {

        mDotView = (DotView) sliderBanner.findViewById(R.id.slider_banner_indicator);

        mSliderBanner = sliderBanner;

        sliderBanner.setAdapter(mBannerAdapter);
    }

    public void play(JsonData list,Boolean isAutoPlay) {
        mBannerAdapter.setData(list);
        mBannerAdapter.notifyDataSetChanged();
        mSliderBanner.setDotNum(list.length(),mBannerAdapter.getPositionForIndicator(mSliderBanner.getCurrentItem()));
        if (isAutoPlay) {
        	mSliderBanner.beginPlay();
		}
    }

    private class InnerAdapter extends BannerAdapter {
        private JsonData mDataList;

        public void setData(JsonData data) {
            mDataList = data;
        }

        public JsonData getItem(int position) {
            if (mDataList == null)
                return null;
            return mDataList.optJson(getPositionForIndicator(position));
        }

        @Override
        public int getPositionForIndicator(int position) {
            if (null == mDataList || mDataList.length() == 0) {
                return 0;
            }
            return position % mDataList.length();
        }

        @SuppressLint("InflateParams")
		@Override
        public View getView(LayoutInflater layoutInflater, final int position) {
        	 final JsonData item = getItem(position);
        	 int p=0;
        	 if (mDataList!=null&&mDataList.length()>0) {
        			p=position%mDataList.length();
			}
        	 View convertView=createView(layoutInflater, p, item);
        	 return convertView;
        }

        @Override
        public int getCount() {
            if (mDataList == null) {
                return 0;
            }
            return Integer.MAX_VALUE;
        }
    }
    
    /**
     * 创建view 抽象函数
     * @param layoutInflater
     * @param position
     * @param mDataList
     * @return
     */
    public abstract View createView(LayoutInflater layoutInflater, final int position,JsonData itemData);
    
}
